package org.itheima.googleplay.ui.fragment;

import java.util.List;

import org.itheima.googleplay.R;
import org.itheima.googleplay.bean.AppInfoBean;
import org.itheima.googleplay.bean.HomeBean;
import org.itheima.googleplay.http.HomeProtocol;
import org.itheima.googleplay.ui.adapter.AppListAdapter;
import org.itheima.googleplay.ui.adapter.SuperBaseAdapter;
import org.itheima.googleplay.ui.fragment.LoadingPager.LoadedResult;
import org.itheima.googleplay.ui.holder.AppItemHolder;
import org.itheima.googleplay.ui.holder.BaseHolder;
import org.itheima.googleplay.ui.holder.HomePictureHolder;
import org.itheima.googleplay.ui.weiget.BaseListView;
import org.itheima.googleplay.utils.LogUtils;
import org.itheima.googleplay.utils.UIUtils;

import android.graphics.Color;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * @包名: org.itheima.googleplay.ui.fragment
 * @类名: HomeFragment
 * @作者: 肖琦
 * @创建时间 : 2015-3-20 下午2:18:28
 * 
 * @描述: TODO
 * 
 * @SVN版本: $Rev: 22 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-26 13:36:49 +0800 (星期四, 26 三月 2015) $
 * 
 * @更新描述:
 */
public class HomeFragment extends BaseFragment
{

	private List<AppInfoBean>	mListDatas; // listView对应的数据
	private List<String>		mPictures;	// 轮播图数据

	private HomeBean			mDataBean;
	private HomeProtocol		mProtocol;
	private HomeAdapter			mAdapter;

	@Override
	protected View onSuccessView()
	{
		// TextView tv = new TextView(UIUtils.getContext());
		// tv.setText("首页");
		// tv.setGravity(Gravity.CENTER);
		// tv.setTextSize(24);
		// return tv;

		BaseListView mListView = new BaseListView(UIUtils.getContext());

		// 加载轮播图
		HomePictureHolder pictureHolder = new HomePictureHolder();
		// 加载头
		mListView.addHeaderView(pictureHolder.getRootView());
		// 给holder数据
		pictureHolder.setData(mPictures);

		// adapter ---> list
		mAdapter = new HomeAdapter(mListView, mListDatas);
		mListView.setAdapter(mAdapter);

		// 开启下载的观察者
		mAdapter.startObserver();

		return mListView;
	}

	@Override
	public void onResume()
	{
		super.onResume();

		if (mAdapter != null)
		{
			mAdapter.startObserver();
		}
	}

	@Override
	public void onPause()
	{
		super.onPause();

		if (mAdapter != null)
		{
			mAdapter.stopObserver();
		}
	}

	@Override
	protected LoadedResult onLoadData()
	{
		// LoadedResult[] results = new LoadedResult[] {
		// LoadedResult.EMPTY,
		// LoadedResult.ERROR,
		// LoadedResult.SUCCESS
		// };
		//
		// try
		// {
		// Thread.sleep(1000);
		// }
		// catch (InterruptedException e)
		// {
		// e.printStackTrace();
		// }
		//
		// Random rdm = new Random();
		// return results[rdm.nextInt(results.length)];

		// 模拟数据加载
		//
		// mDatas = new LinkedList<String>();
		// for (int i = 0; i < 50; i++)
		// {
		// mDatas.add("" + i);
		// }
		//
		// // 去网络获取数据
		// HttpUtils utils = new HttpUtils();
		// String url = "http://10.0.2.2:8080/GooglePlayServer/home";
		//
		// RequestParams params = new RequestParams();
		// params.addQueryStringParameter("index", 0 + "");
		//
		// try
		// {
		// ResponseStream stream = utils.sendSync(HttpMethod.GET, url, params);
		//
		// int statusCode = stream.getStatusCode();
		// if (200 == statusCode)
		// {
		// // 正确返回
		// String json = stream.readString();
		// LogUtils.d(json);
		//
		// // 解析json
		// Gson gson = new Gson();
		// mDataBean = gson.fromJson(json, HomeBean.class);
		//
		// if (mDataBean == null) { return LoadedResult.EMPTY; }
		//
		// mListDatas = mDataBean.list;
		// mPictures = mDataBean.picture;
		//
		// if (mListDatas == null || mListDatas.size() == 0) { return
		// LoadedResult.EMPTY; }
		// }
		// else
		// {
		// return LoadedResult.ERROR;
		// }
		// }
		// catch (Exception e)
		// {
		// e.printStackTrace();
		// return LoadedResult.ERROR;
		// }
		//
		// return LoadedResult.SUCCESS;

		mProtocol = new HomeProtocol();

		try
		{
			mDataBean = mProtocol.loadData(0);

			if (mDataBean == null) { return LoadedResult.EMPTY; }

			LoadedResult state = checkState(mDataBean);
			if (state == LoadedResult.EMPTY) { return state; }

			mListDatas = mDataBean.list;
			mPictures = mDataBean.picture;
			return checkState(mListDatas);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return LoadedResult.ERROR;
		}

	}

	class HomeAdapter extends AppListAdapter
	{

		public HomeAdapter(AbsListView listView, List<AppInfoBean> datas) {
			super(listView, datas);
		}

		@Override
		protected List<AppInfoBean> onLoadMoreData() throws Exception
		{
			return loadMoreData(mDatas.size());
		}
	}

	private List<AppInfoBean> loadMoreData(int index) throws Exception
	{
		// HttpUtils utils = new HttpUtils();
		// String url = "http://10.0.2.2:8080/GooglePlayServer/home";
		//
		// RequestParams params = new RequestParams();
		// params.addQueryStringParameter("index", index + "");
		//
		// ResponseStream stream = utils.sendSync(HttpMethod.GET, url, params);
		//
		// int statusCode = stream.getStatusCode();
		// if (200 == statusCode)
		// {
		// // 正确返回
		// String json = stream.readString();
		// LogUtils.d(json);
		//
		// // 解析json
		// Gson gson = new Gson();
		// HomeBean bean = gson.fromJson(json, HomeBean.class);
		//
		// if (bean == null) { return null; }
		//
		// return bean.list;
		// }
		// else
		// {
		// return null;
		// }
		HomeBean bean = mProtocol.loadData(index);
		return bean.list;
	}

}
